package com.qf.user.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.core.dto.PageDto;
import com.qf.core.vo.PageResult;
import com.qf.core.vo.R;
import com.qf.user.entity.User;
import com.qf.user.mapper.MenuMapper;
import com.qf.user.mapper.RoleMapper;
import com.qf.user.mapper.UserMapper;
import com.qf.user.service.UserService;
import com.qf.user.util.JWTUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 管理员用户表 服务实现类
 * </p>
 *
 * @author yxh
 * @since 2023-10-29
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {


    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public R login(User user) {

        if(user==null){
            return R.fail("请输入用户名和密码");
        }

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(User::getUserName,"admin");
        User userDb = this.baseMapper.selectOne(queryWrapper);

        if (userDb == null) {
            return R.fail("用户名或者密码不正确");
        }

        //比较密码
        String md5 = SecureUtil.md5(user.getPassword());
        if(!md5.equals(userDb.getPassword())){
            return R.fail("用户名或者密码不正确");
        }

        //获取用户的权限列表
        List<String> menus = menuMapper.findMenuById(userDb.getId() + "");
        userDb.setMenus(menus);


        //获取用户的角色列表
        List<String> roles = roleMapper.findRoleByUserId(userDb.getId() + "");
        userDb.setRoles(roles);

        //颁发jwt令牌
        userDb.setPassword("");
        String token = JWTUtil.generatorHoursToken(userDb, 2);


        return R.ok(token,"登录成功");

    }

    @Override
    public PageResult<User> findPage(PageDto<User> pageDto) {
        IPage<User> page = new Page<>(pageDto.getNow(),pageDto.getSize());
        User where = pageDto.getWhere();
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        //根据昵称查询
        if(!StringUtils.isEmpty(where.getNickName())){
            queryWrapper.lambda().like(User::getNickName,where.getNickName());
        }
        //根据电话查询
        if(!StringUtils.isEmpty(where.getPhone())){
            queryWrapper.lambda().like(User::getPhone,where.getPhone());
        }
        if(!StringUtils.isEmpty(where.getPhone())){
            queryWrapper.lambda().like(User::getPhone,where.getPhone());
        }
        IPage<User> PageInfo = this.baseMapper.selectPage(page, queryWrapper);

        PageResult<User> pageResult = new PageResult<>();
        pageResult.setPages(PageInfo.getPages());
        pageResult.setList(PageInfo.getRecords());
        pageResult.setTotal(PageInfo.getTotal());
        return pageResult;
    }

    @Override
    public R add(User user) {
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        user.setEnable(1);
        int i = this.baseMapper.insert(user);
        if (i>0){
            return R.ok("添加成功");
        }
        return R.fail("添加失败");
    }
}
